package wb_view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Calendar;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.table.DefaultTableModel;

import model.ParkingAreaManager;
import util.CheckExceptionHelper;
import vo.ParkHistory;
import vo.ParkInfo;
import exception.FormatException;
import exception.InvalidCarNumException;
import exception.NotAvailException;
import exception.SpecialCharacterException;

public class MainFrame extends JFrame {

	private JPanel contentPane;
	private JTabbedPane tabbedPane;
	private JPanel jpaneIO;
	private JPanel jpaneCurrent;
	private JLabel jlabelCurrentInfo;
	private JLabel jlabelAreaCount;
	private JLabel jlabelParkCount;
	private JLabel jlabelAvailCount;
	private JLabel jlabelDate2;
	private JLabel jlabelCarNum;
	private JLabel jlabelPhone;
	private JLabel jlabelTime;
	private JTextField jfieldDate;
	private JTextField jfieldCarNum;
	private JTextField jfieldPhone;
	private JTextField jfieldTime;
	private JButton jbtnIn;
	private JButton jbtnOut;
	private JButton jbtnSearchAvailArea;
	private JButton jbtnSearchCarLoc;
	private JLabel jlabelParkArea;
	private JTextField jfieldParkArea;
	private JScrollPane scrollPane;
	private JTextField jfieldArea;
	private JTextField jfieldParkCount;
	private JTextField jfieldAvailCount;
	private JTable table;
	private String[] table_header= {"주차구역 No.","주차여부."};
	private String[] combo_date = {"2013/05/24","2013/05/25","2013/05/26","2013/05/27","2013/05/28",
			"2013/05/29","2013/05/30","2013/05/31","2013/06/01","2013/06/02","2013/06/03","2013/06/04",
			"2013/06/05","2013/06/06","2013/06/07","2013/06/08","2013/06/09","2013/06/10","2013/06/11",
			"2013/06/12","2013/06/13","2013/06/14","2013/06/15","2013/06/16","2013/06/17","2013/06/18",
			"2013/06/19","2013/06/20","2013/06/21","2013/06/22","2013/06/23","2013/06/24"};

	private String[] combo_parking_area = {"IT-1","IT-2","IT-3","IT-4","IT-5","IT-6","IT-7","IT-8",
			"IT-9","IT-10","IT-11","IT-12","IT-13","IT-14","IT-15","IT-16","IT-17","IT-18","IT-19","IT-20"};


	private JLabel jlabelDate;
	private JComboBox comboBoxDate;
	private JLabel jlabelArea;
	private JComboBox comboBoxArea;
	private JButton jbtnSearchDetail;
	private JLabel jlabelcurrentDate;



	/**
	 * Create the frame.
	 */
	public MainFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 600, 700);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
		tabbedPane.setBounds(0,0, 550, 600);
		contentPane.add(tabbedPane);

		jpaneCurrent = new JPanel();
		tabbedPane.addTab("\uC8FC\uCC28\uD604\uD669", null, jpaneCurrent, null);
		jpaneCurrent.setLayout(null);

		jlabelCurrentInfo = new JLabel("\uC2E4\uC2DC\uAC04 \uC8FC\uCC28 \uC815\uBCF4");
		jlabelCurrentInfo.setHorizontalAlignment(SwingConstants.CENTER);
		jlabelCurrentInfo.setBounds(213, 26, 108, 17);
		jlabelCurrentInfo.setFont(UIManager.getFont("Button.font"));
		jpaneCurrent.add(jlabelCurrentInfo);

		jlabelAreaCount = new JLabel("\uC8FC\uCC28\uBA74\uC218");
		jlabelAreaCount.setBounds(79, 69, 57, 15);
		jpaneCurrent.add(jlabelAreaCount);

		jlabelParkCount = new JLabel("\uC8FC\uCC28\uB300\uC218");
		jlabelParkCount.setBounds(239, 69, 57, 15);
		jpaneCurrent.add(jlabelParkCount);

		jlabelAvailCount = new JLabel("\uAC00\uC6A9\uB300\uC218");
		jlabelAvailCount.setBounds(415, 69, 57, 15);
		jpaneCurrent.add(jlabelAvailCount);

		scrollPane = new JScrollPane(table);
		scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
		scrollPane.setBounds(50, 150, 450,200);
		jpaneCurrent.add(scrollPane);



		jfieldArea = new JTextField();
		jfieldArea.setBounds(49, 104, 116, 21);
		jpaneCurrent.add(jfieldArea);
		jfieldArea.setColumns(10);

		jfieldParkCount = new JTextField();
		jfieldParkCount.setBounds(209, 104, 116, 21);
		jpaneCurrent.add(jfieldParkCount);
		jfieldParkCount.setColumns(10);

		jfieldAvailCount = new JTextField();
		jfieldAvailCount.setBounds(383, 104, 116, 21);
		jpaneCurrent.add(jfieldAvailCount);
		jfieldAvailCount.setColumns(10);

		jlabelDate = new JLabel("\uB0A0\uC9DC");
		jlabelDate.setBounds(50, 388, 57, 15);
		jpaneCurrent.add(jlabelDate);

		comboBoxDate = new JComboBox(combo_date);
		comboBoxDate.setBounds(102, 385, 100, 21);
		jpaneCurrent.add(comboBoxDate);

		jlabelArea = new JLabel("\uC8FC\uCC28\uAD6C\uC5ED");
		jlabelArea.setBounds(209, 388, 57, 15);
		jpaneCurrent.add(jlabelArea);

		comboBoxArea = new JComboBox(combo_parking_area);
		comboBoxArea.setBounds(282, 385, 85, 21);
		jpaneCurrent.add(comboBoxArea);

		jbtnSearchDetail = new JButton("\uC0C1\uC138\uC815\uBCF4\uC870\uD68C");
		jbtnSearchDetail.setBounds(383, 384, 117, 23);
		jpaneCurrent.add(jbtnSearchDetail);

		jlabelcurrentDate = new JLabel("New label");
		jlabelcurrentDate.setBounds(370, 26, 130, 17);

		Calendar cal = Calendar.getInstance();
		String month;
		if((cal.get(Calendar.MONTH)+1)<10)
			month = "0"+ cal.get((Calendar.MONTH+1));
		else
			month = "" +cal.get((Calendar.MONTH+1));
		jlabelcurrentDate.setText("날짜 : " + cal.get(Calendar.YEAR) +"/"+(cal.get(Calendar.MONTH)+1)
				+"/"+cal.get(Calendar.DAY_OF_MONTH));
		jpaneCurrent.add(jlabelcurrentDate);

		jpaneIO = new JPanel();
		tabbedPane.addTab("\uC785/\uCD9C\uCC28\uC815\uBCF4", null, jpaneIO, null);
		jpaneIO.setLayout(null);

		jlabelDate2 = new JLabel("\uB0A0 \uC9DC");
		jlabelDate2.setHorizontalAlignment(SwingConstants.CENTER);
		jlabelDate2.setBounds(100,60, 57, 15);
		jpaneIO.add(jlabelDate2);

		jlabelCarNum = new JLabel("\uCC28\uB7C9\uBC88\uD638");
		jlabelCarNum.setHorizontalAlignment(SwingConstants.CENTER);
		jlabelCarNum.setBounds(100, 120, 57, 15);
		jpaneIO.add(jlabelCarNum);

		jlabelPhone = new JLabel("\uC804\uD654\uBC88\uD638");
		jlabelPhone.setHorizontalAlignment(SwingConstants.CENTER);
		jlabelPhone.setBounds(100,180, 57, 15);
		jpaneIO.add(jlabelPhone);

		jlabelTime = new JLabel("\uC2DC \uAC04");
		jlabelTime.setHorizontalAlignment(SwingConstants.CENTER);
		jlabelTime.setBounds(100, 240, 57, 15);
		jpaneIO.add(jlabelTime);

		jlabelParkArea = new JLabel("\uC8FC\uCC28\uAD6C\uC5ED");
		jlabelParkArea.setHorizontalAlignment(SwingConstants.CENTER);
		jlabelParkArea.setBounds(100, 300, 57, 15);
		jpaneIO.add(jlabelParkArea);

		jfieldDate = new JTextField();
		jfieldDate.setBounds(198, 50, 116, 40);
		jpaneIO.add(jfieldDate);
		jfieldDate.setColumns(10);

		jfieldCarNum = new JTextField();
		jfieldCarNum.setBounds(198, 110, 116, 40);
		jpaneIO.add(jfieldCarNum);
		jfieldCarNum.setColumns(10);

		jfieldPhone = new JTextField();
		jfieldPhone.setBounds(198, 170, 116, 40);
		jpaneIO.add(jfieldPhone);
		jfieldPhone.setColumns(10);

		jfieldTime = new JTextField();
		jfieldTime.setBounds(198, 230, 116, 40);
		jpaneIO.add(jfieldTime);
		jfieldTime.setColumns(10);

		jfieldParkArea = new JTextField();
		jfieldParkArea.setBounds(198, 290, 116, 40);
		jpaneIO.add(jfieldParkArea);
		jfieldParkArea.setColumns(10);

		jbtnIn = new JButton("\uC785\uCC28");
		jbtnIn.setBounds(342, 50, 140, 40);
		jpaneIO.add(jbtnIn);

		jbtnOut = new JButton("\uCD9C\uCC28");
		jbtnOut.setBounds(342, 110, 140, 40);
		jpaneIO.add(jbtnOut);

		jbtnSearchAvailArea = new JButton("\uC8FC\uCC28\uAC00\uB2A5\uAD6C\uC5ED\uC870\uD68C");
		jbtnSearchAvailArea.setFont(UIManager.getFont("Button.font"));
		jbtnSearchAvailArea.setBounds(342, 170, 140, 40);
		jpaneIO.add(jbtnSearchAvailArea);

		jbtnSearchCarLoc = new JButton("\uCC28\uB7C9\uC704\uCE58\uC870\uD68C");
		jbtnSearchCarLoc.setBounds(342, 230, 140, 40);
		jpaneIO.add(jbtnSearchCarLoc);

		setParkInfoComponent();

		ActionListener al = new ActionListener()
		{
			public void actionPerformed(ActionEvent ae)
			{

				if(ae.getSource()==jbtnSearchDetail)//상세정보조회 버튼  
				{
					String date = comboBoxDate.getSelectedItem().toString();
					String area = comboBoxArea.getSelectedItem().toString();

					DetailedInformation frame = new DetailedInformation(date, area);

					frame.setVisible(true);

				}

				else if(ae.getSource()==jbtnIn)//입차 버튼 
				{
					
					final ParkInfo parkInfo = new ParkInfo();			
			
					
					ParkHistory history = new ParkHistory();
					history.setInDate(jfieldDate.getText());
					history.setInTime(jfieldTime.getText());
					history.setCarNum(jfieldCarNum.getText());				
			
					parkInfo.addHistory(history);
					
					parkInfo.setParked(true);
					parkInfo.setCarNumber(jfieldCarNum.getText());
					parkInfo.setPhone(jfieldPhone.getText());
					parkInfo.setAreaName(jfieldParkArea.getText());

					try {
						CheckExceptionHelper.isValidPhone(jfieldPhone.getText());
					} catch (FormatException e3) {
						e3.printStackTrace();
					}
					try {
						CheckExceptionHelper.isValidDate(jfieldDate.getText());
					} catch (SpecialCharacterException e2) {
						e2.printStackTrace();
					}
					try {
						CheckExceptionHelper.isValidCarNum(jfieldCarNum.getText());
					} catch (InvalidCarNumException e1) {
						e1.printStackTrace();
					}
					try {
						CheckExceptionHelper.isValidTime(jfieldTime.getText());
					} catch (SpecialCharacterException e) {
						e.printStackTrace();
					}

					
				
				
					Thread thread = new Thread(){

						public void run() {											
								ParkingAreaManager.getInstance().addParkInfo(parkInfo);
							}
						
					};
					
					thread.start();
				}


				else if(ae.getSource()==jbtnOut)//출차 버튼  
				{
					final String areaName = jfieldParkArea.getText();
					final String outDate = jfieldDate.getText();
					final String outTime = jfieldTime.getText();

					Thread thread = new Thread() {

						public void run() {
							ParkingAreaManager.getInstance().removeParkInfo(areaName,outDate,outTime);	
						};

					};

				
					thread.start();
				}


				else if(ae.getSource()==jbtnSearchAvailArea)//주차가능구역 조회 버튼 
				{
					AvailableArea frame1 = new AvailableArea();
					frame1.setVisible(true);
				}


				else if(ae.getSource()==jbtnSearchCarLoc)//차량위치조회 버튼  
				{
					CarLocation frame2 = new CarLocation(jfieldCarNum.getText());
					frame2.setVisible(true);
				}

			}


		};


		jbtnSearchDetail.addActionListener(al);
		jbtnIn.addActionListener(al);
		jbtnOut.addActionListener(al);
		jbtnSearchAvailArea.addActionListener(al);
		jbtnSearchCarLoc.addActionListener(al);

		tabbedPane.addChangeListener(new ChangeListener() {

			@Override
			public void stateChanged(ChangeEvent e) {

				JPanel panel = (JPanel) tabbedPane.getSelectedComponent();

				if(panel.equals(jpaneCurrent)) {
					setParkInfoComponent();
				}

			}
		});

	}
	private void setParkInfoComponent() {

		DefaultTableModel model = new DefaultTableModel(table_header,0);
		table = new JTable(model);
		scrollPane.setViewportView(table);

		int areaCount = ParkingAreaManager.getInstance().getAreaSize();
		int parkCount = ParkingAreaManager.getInstance().getParkCount();
		int availCount = ParkingAreaManager.getInstance().getAvailCount();

		jfieldArea.setText(String.valueOf(areaCount));
		jfieldParkCount.setText(String.valueOf(parkCount));
		jfieldAvailCount.setText(String.valueOf(availCount));

		ArrayList<ParkInfo> list = 	ParkingAreaManager.getInstance().getParkingList();

		for(int i = 0; i < list.size(); ++i) {

			String[] contents = new String[2];

			contents[0] = list.get(i).getAreaName();

			if(!list.get(i).isParked())
				contents[1] = "vacancy";
			else
				contents[1] = "parking";	    		

			model.addRow(contents);
		}

	}

}
